﻿package org.libspark.betweenas3.core.tweens
{
	import org.libspark.betweenas3.core.easing.IEasing;
	import org.libspark.betweenas3.core.ticker.ITicker;
	import org.libspark.betweenas3.core.updaters.IUpdater;
	import org.libspark.betweenas3.tweens.ITween;
	
	/**
	 * .
	 * 
	 * @author	yossy:beinteractive
	 */
	public class ObjectTween extends AbstractTween implements IIObjectTween
	{
		public function ObjectTween(ticker:ITicker)
		{
			super(ticker, 0);
		}
		
		protected var _easing:IEasing;
		protected var _updater:IUpdater;
		

		public function get time():Number { return _duration; }
		public function set time(value:Number):void { _duration = value; }

		public function get easing():IEasing { return _easing; }
		public function set easing(value:IEasing):void { _easing = value; }

		public function get updater():IUpdater { return _updater; }
		public function set updater(value:IUpdater):void { _updater = value; }

        
		public function get target():Object
		{
			return _updater != null ? _updater.target : null;
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function internalUpdate(time:Number):void 
		{
			var factor:Number = 0.0;
			
			if (time > 0.0) {
				if (time < _duration) {
					factor = _easing.calculate(time, 0.0, 1.0, _duration);
				}
				else {
					factor = 1.0;
				}
			}
			
			_updater.update(factor);
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function newInstance():AbstractTween 
		{
			return new ObjectTween(_ticker);
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function copyFrom(source:AbstractTween):void 
		{
			super.copyFrom(source);
			
			var obj:ObjectTween = source as ObjectTween;
			
			_easing = obj._easing;
			_updater = obj._updater.clone();
		}
	}
}